//
//  MSUserCenterDataSource.m
//  MSJD
//
//  Created by 箭军刘 on 2018/12/2.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSUserCenterDataSource.h"

#import "NSString+YYAdd.h"

@implementation MSUserCenterDataSource

+ (void)applyForTenantsWithBusinessAddress:(NSString *)businessAddress
                             businessTitle:(NSString *)busineesTitle
                               contactName:(NSString *)contactName
                              contactPhone:(NSString *)contactPhone
                            uploadIconList:(NSArray *)uploadIconList
                                   success:(void (^)(NSDictionary *responseInfo))success
                                   failure:(void (^)(NSError *error))failure {
    NSString *requestUrl = @"/v1/item/register";
    
    NSDictionary *param = @{@"location":[MSSafeObject stringInfo:businessAddress],
                            @"itemName":[MSSafeObject stringInfo:busineesTitle],
                            @"owner":[MSSafeObject stringInfo:contactName],
                            @"phone":[MSSafeObject stringInfo:contactPhone],
                            @"kind":@(0),
                            @"covers":[uploadIconList objectAtIndex:0],
                            @"photos":[uploadIconList objectAtIndex:1],
                            @"idcardPos":[uploadIconList objectAtIndex:2],
                            @"idcardNeg":[uploadIconList objectAtIndex:3],
                            @"busLicense":[uploadIconList objectAtIndex:4],
                            @"operLicense":[uploadIconList objectAtIndex:5]};
    [MSRequestClient POST:requestUrl parameters:param responseClassType:NSDictionary.class responseCache:^(id responseCache) {
        
    } success:^(id responseObject) {
        if (success) {
            success((NSDictionary *)responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)queryUploadImageConfigWithSuccess:(void (^)(NSDictionary *responseInfo))success
                                  failure:(void (^)(NSError *error))failure {
    NSString *requestUrl = @"v1/oss/token";
    MSUser *loginUser = [UserAccesser loadAccount];
    NSNumber *uid = loginUser.userId;
    NSString *token = [MSSafeObject stringInfo:loginUser.token];
    NSString *sign = [NSString stringWithFormat:@"%@%@mjms@mgtv", uid, token].md5String;
    
    NSDictionary *param = @{@"uid":loginUser.userId?:@"", @"token":[MSSafeObject stringInfo:loginUser.token], @"sign":sign};
    [MSNetworkAPI GET:requestUrl domain:API_TOPHRMING parameters:param success:^(id responseObject) {
        NSDictionary *responseData = (NSDictionary *)responseObject;
        if (!responseData ||
            ![responseData isKindOfClass:NSDictionary.class]) {
            if (failure) {
                failure(nil);
            }
            return ;
        }
        NSDictionary *result = responseData;
        if ([[result allKeys] containsObject:@"data"]) {
            result = [MSSafeObject dictionaryInfo:result[@"data"]];
        }
        if (success) {
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)modifyUserInfoWithAvatar:(NSString *)avatar
                        userName:(NSString *)userName
                          gender:(NSInteger)gender
                         success:(void (^)(NSDictionary *responseInfo))success
                         failure:(void (^)(NSError *error))failure {
    NSString *requestUrl = @"v1/user/update";
    NSInteger uid = [UserAccesser hadDefaultAccount]?[[UserAccesser loadAccount].userId integerValue]:0;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@(uid) forKey:@"uid"];
    if (![MSSafeObject isNull:userName]) {
        [param setObject:userName forKey:@"username"];
    }
    if (gender > 0) {
        [param setObject:@(gender) forKey:@"gender"];
    }
    if (![MSSafeObject isNull:avatar]) {
        [param setObject:avatar forKey:@"avatar"];
    }
    [MSNetworkAPI POST:requestUrl domain:API_TOPHRMING parameters:param.copy isJsonData:NO success:^(id responseObject) {
        if (success) {
            success((NSDictionary *)responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
